package com.ylbh.modules.businessorder.service.impl;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.ylbh.common.exception.NormalException;
import com.ylbh.common.pojo.businessorder.dto.BusinessOrderDTO;
import com.ylbh.common.pojo.businessorder.vo.*;
import com.ylbh.common.utils.*;
import com.ylbh.modules.businessorder.entity.ShoppingBusinessOrder;
import com.ylbh.modules.businessorder.mapper.ShoppingBusinessOrderMapper;
import com.ylbh.modules.businessorder.service.ShoppingBusinessOrderService;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;


@Service
public class ShoppingBusinessOrderServiceImpl extends ServiceImpl<ShoppingBusinessOrderMapper, ShoppingBusinessOrder> implements ShoppingBusinessOrderService {

    @Autowired
    private ShoppingBusinessOrderMapper shoppingBusinessOrderMapper;

    @Value("${url.orderWriteOff}")
    private String orderWriteOff;

    /**
     * 联盟店订单详情
     * @param id
     * @param payNo
     * @return
     */
    @Override
    public BusinessOrderDetailsVO getOrderByIdOrPayNo(Long id, String payNo, String orderType) {
        if (!StringUtils.isEmpty(orderType)) {
            if ("线上订单".equals(orderType)) {
                // 成立则是通过线上生成订单
                BusinessOrderDetailsVO byId = shoppingBusinessOrderMapper.selectOrderById(id);
                String goodsName = shoppingBusinessOrderMapper.selectGoodsNameByPayNo(payNo);
                if (!StringUtils.isEmpty(goodsName))
                    byId.setGoodNames(goodsName);
                return byId;
            } else if ("买单".equals(orderType) || "扫码".equals(orderType)) {
                // 成立则是通过线下联盟店买单
                BusinessOrderDetailsVO byId = shoppingBusinessOrderMapper.selectPayInfoById(id);
                return byId;
            }
        }
        return null;
    }

    /**
     * 树形联盟店订单列表
     * @param businessOrderDTO
     * @return
     */
    @Override
    public PageUtils queryOrderTreeList(BusinessOrderDTO businessOrderDTO) {
        Page<Object> page = new Page<>(businessOrderDTO.getPage(), businessOrderDTO.getLimit());
        List<BusinessOrderListVO> list = shoppingBusinessOrderMapper.selectBusinessOrderList(businessOrderDTO, page);
        for (BusinessOrderListVO b : list) {
            if (null != b.getPayNo()) {
                List<SubOrderVO> subOrderVos = shoppingBusinessOrderMapper.selectSonOrderList(b.getPayNo());
                b.setList(subOrderVos);
            }
        }
        Page<BusinessOrderListVO> page1 = new Page<>();
        page1.setTotal(page.getTotal());
        page1.setRecords(list);
        return new PageUtils(page1);
    }

    /**
     * 子订单详情
     * @param payNo
     * @return
     */
    @Override
    public SubOrderDetailsVO getOrderByPayNO(String payNo) {
        SubOrderDetailsVO sonOrderDetails = shoppingBusinessOrderMapper.selectSonOrderDetails(payNo);
        if (null != sonOrderDetails.getGoodsImage())
            sonOrderDetails.setImg("http://image.yilianbaihui.cn/consumer/goods/" + sonOrderDetails.getGoodsImage());
        return sonOrderDetails;
    }

    /**
     * 导出联盟店订单
     * @param response
     * @throws NormalException
     */
    @Override
    public void businessOrderExcel(HttpServletResponse response, HttpServletRequest request) throws NormalException {
        String payStatus = request.getParameter("payStatus");
        String payType = request.getParameter("payType");
        String startTime = request.getParameter("startTime");
        String endTime = request.getParameter("endTime");
        String keywords = request.getParameter("keywords");
        List<BusinessOrderExcelVO> excel = shoppingBusinessOrderMapper.selectBusinessOrderExcel(payStatus, payType, startTime, endTime, keywords);
        for (BusinessOrderExcelVO vo : excel) {
            if (!StringUtils.isEmpty(vo.getTypeName()) && "运营商".equals(vo.getTypeName()) && !StringUtils.isEmpty(vo.getUserName())) {
                vo.setRealName(shoppingBusinessOrderMapper.selectRealNameByUserName(vo.getUserName()));
            }else if (!StringUtils.isEmpty(vo.getnTypeName()) && "运营商".equals(vo.getnTypeName()) && !StringUtils.isEmpty(vo.getnUserName())) {
                vo.setRealName(shoppingBusinessOrderMapper.selectRealNameByUserName(vo.getnUserName()));
            }
            if (StringUtils.isEmpty(vo.getnUserName()))
                vo.setRealName(" ");
        }
        ExcelUtils.exportExcel(excel, "联盟店订单", "", BusinessOrderExcelVO.class, System.currentTimeMillis() + ".xls", response);
    }

    /**
     * 批量修改联盟店订单状态
     * @param payNo
     * @return
     */
    @Override
    public String modifyBatchOrderStatusById(String payNo) {
        HashMap<String, String> map = new HashMap<>();
        map.put("oldOrderState", "1");
        map.put("newOrderState", "2");
        map.put("orderNos", payNo);
        return HttpClientHelperUtils.sendHttpPost(orderWriteOff, map);
    }

    /**
     * 联盟店详情-新版
     * @param id
     * @param payNo
     * @param orderType
     * @return
     */
    @Override
    public NewBusinessOrderDetailsVO getNewOrderByIdOrPayNo(Long id, String payNo, String orderType) {
        NewBusinessOrderDetailsVO vo = null;
        if ("线上订单".equals(orderType)) {
            vo = shoppingBusinessOrderMapper.selectNewBusinessOrder(id);
            // 查询该笔订单里的商品
            List<NewBusinessGoodsVO> list = shoppingBusinessOrderMapper.selectNewOrderGoods(vo.getOrderNo());
            vo.setList(list);
        } else {
            vo =shoppingBusinessOrderMapper.selectNewPayInfoById(payNo);
        }
        return vo;
    }
}
